package com.ruoyi.web.controller.customer;

import java.util.List;


import com.ruoyi.system.domain.HhUserAccount;
import com.ruoyi.system.service.IHhUserAccountService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户余额变动Controller
 * 
 * @author ruoyi
 * @date 2021-06-16
 */
@RestController
@RequestMapping("/system/account")
public class HhUserAccountController extends BaseController
{
    @Autowired
    private IHhUserAccountService hhUserAccountService;

    /**
     * 查询用户余额变动列表
     */
    @PreAuthorize("@ss.hasPermi('system:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(HhUserAccount hhUserAccount)
    {
        startPage();
        List<HhUserAccount> list = hhUserAccountService.selectHhUserAccountList(hhUserAccount);
        return getDataTable(list);
    }

    /**
     * 导出用户余额变动列表
     */
    @PreAuthorize("@ss.hasPermi('system:account:export')")
    @Log(title = "用户余额变动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HhUserAccount hhUserAccount)
    {
        List<HhUserAccount> list = hhUserAccountService.selectHhUserAccountList(hhUserAccount);
        ExcelUtil<HhUserAccount> util = new ExcelUtil<HhUserAccount>(HhUserAccount.class);
        return util.exportExcel(list, "用户余额变动");
    }

    /**
     * 获取用户余额变动详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:account:query')")
    @GetMapping(value = "/{uaId}")
    public AjaxResult getInfo(@PathVariable("uaId") Long uaId)
    {
        return AjaxResult.success(hhUserAccountService.selectHhUserAccountById(uaId));
    }

    /**
     * 新增用户余额变动
     */
    @PreAuthorize("@ss.hasPermi('system:account:add')")
    @Log(title = "用户余额变动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HhUserAccount hhUserAccount)
    {
        double before = Double.parseDouble(hhUserAccount.getBeforeBalance());
        double now = Double.parseDouble(hhUserAccount.getThisAmount());
        double after = Double.parseDouble(hhUserAccount.getAfterBalance());
        if (hhUserAccount.getEvenId().equals("11") && after!=(now+before)){
            return AjaxResult.error("操作失败");
        }
        if (hhUserAccount.getEvenId().equals("12") && before!=(now+after)){
            return AjaxResult.error("操作失败");
        }
        return toAjax(hhUserAccountService.insertHhUserAccount(hhUserAccount));
    }

    /**
     * 修改用户余额变动
     */
    @PreAuthorize("@ss.hasPermi('system:account:edit')")
    @Log(title = "用户余额变动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HhUserAccount hhUserAccount)
    {
        double before = Double.parseDouble(hhUserAccount.getBeforeBalance());
        double now = Double.parseDouble(hhUserAccount.getThisAmount());
        double after = Double.parseDouble(hhUserAccount.getAfterBalance());
        if (hhUserAccount.getEvenId().equals("11") && after!=(now+before)){
            return AjaxResult.error("操作失败");
        }
        if (hhUserAccount.getEvenId().equals("12") && before!=(now+after)){
            return AjaxResult.error("操作失败");
        }
        return toAjax(hhUserAccountService.updateHhUserAccount(hhUserAccount));
    }

    /**
     * 删除用户余额变动
     */
    @PreAuthorize("@ss.hasPermi('system:account:remove')")
    @Log(title = "用户余额变动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uaIds}")
    public AjaxResult remove(@PathVariable Long[] uaIds)
    {
        return toAjax(hhUserAccountService.deleteHhUserAccountByIds(uaIds));
    }
}
